package com.my.collection.graph.v1;

/**
 * Created by ZhongGang
 * at 2018/10/12 10:08
 */
public class Stack<E> {
    private Node<E> head;

    /**
     * 入栈
     *
     * @param e
     */
    public void push(E e) {
        Node<E> node = new Node<>(e, null);
        if (this.head == null) {
            this.head = node;
        } else {
            Node<E> tmp = this.head;
            this.head = node;
            node.next = tmp;
        }
    }

    /**
     * 出栈
     *
     * @return
     */
    public E pop() {
        if (this.head == null) {
            return null;
        }

        E data = this.head.data;
        this.head = this.head.next;
        return data;
    }

    /**
     * 判断是否是空栈
     *
     * @return
     */
    public boolean isEmpty() {
        return this.head == null;
    }

    private static class Node<E> {
        E data;
        Node<E> next;

        Node(E data, Node<E> next) {
            this.data = data;
            this.next = next;
        }
    }

}
